package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStoragePickingDetailActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStoragePickingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class PickingOutStorageItemListAdapter extends BaseAdapter {

    private String IS_BUILDING_PROJECT_TRUE = "1";

    private Context mContext;
    private List<OutStoragePickingOverviewBean.DataBean.MaterielListBean> mPickingOutStorageItemList;
    private LayoutInflater inflater;
    private OutStoragePickingOverviewActivity.OutStoragePickingOverviewListener mOutStoragePickingListener;
    private String mMaterielCode, mPickingNumber, mApplicantCompany, mApplicantDepartment, mPickingApplicant, mMaterielType, mMoveType, mPickingClassify, mUseDepartment, mObjectDescription, mRemark, mIsBuildingProject;

    public PickingOutStorageItemListAdapter(Context context, OutStoragePickingOverviewBean.DataBean data, List<OutStoragePickingOverviewBean.DataBean.MaterielListBean> outStoragePickingItemList, String materielCode, OutStoragePickingOverviewActivity.OutStoragePickingOverviewListener outStoragePickingOverviewListener) {
        mContext = context;
        mPickingNumber = data.getPickingNumber();
        mApplicantCompany = data.getApplicantCompany();
        mApplicantDepartment = data.getApplicantDepartment();
        mPickingApplicant = data.getPickingApplicant();
        mMaterielType = data.getMaterielTypeCode() + " - " + data.getMaterielTypeValue();
        mMoveType = data.getMoveTypeCode() + " - " + data.getMoveTypeValue();
        mPickingClassify = data.getPickingClassify();
        mUseDepartment = data.getUseDepartment();
        mObjectDescription = data.getObjectDescription();
        mRemark = data.getRemark();
        mIsBuildingProject = data.getIsBuildingProject();
//        mIsFocusBatch = data.getIsFocusBatch();
        mPickingOutStorageItemList = outStoragePickingItemList;
        mOutStoragePickingListener = outStoragePickingOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mPickingOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mPickingOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list_first_item, null);
            TextView pickingNumber = (TextView) convertView.findViewById(R.id.pickingNumber);
            TextView applicantCompany = (TextView) convertView.findViewById(R.id.applicantCompany);
            TextView applicantDepartment = (TextView) convertView.findViewById(R.id.applicantDepartment);
            TextView pickingApplicant = (TextView) convertView.findViewById(R.id.pickingApplicant);
            TextView materielType = (TextView) convertView.findViewById(R.id.materielType);
            TextView moveType = (TextView) convertView.findViewById(R.id.moveType);
            TextView costObject = (TextView) convertView.findViewById(R.id.costObject);
            TextView objectDescription = (TextView) convertView.findViewById(R.id.objectDescription);
            TextView remark = (TextView) convertView.findViewById(R.id.remark);
            TextView isBuildingProject = (TextView) convertView.findViewById(R.id.isBuildingProject);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            pickingNumber.setText(mPickingNumber);
            applicantCompany.setText(mApplicantCompany);
            applicantDepartment.setText(mApplicantDepartment);
            pickingApplicant.setText(mPickingApplicant);
            materielType.setText(mMaterielType);
            moveType.setText(mMoveType);
            costObject.setText(mPickingClassify);
            objectDescription.setText(mObjectDescription);
            remark.setText(mRemark);
            isBuildingProject.setText(IS_BUILDING_PROJECT_TRUE.equals(mIsBuildingProject) ? "是" : "否");
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mOutStoragePickingListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStoragePickingListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            OutStoragePickingOverviewBean.DataBean.MaterielListBean PickingOutstorageItem = mPickingOutStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, PickingOutstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.canSendNumber = (TextView) view.findViewById(R.id.canSendNumber);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStoragePickingOverviewBean.DataBean.MaterielListBean pickingOutStorageItem) {
        viewHolder.serialNumber.setText(pickingOutStorageItem.getSerial());
        viewHolder.materielCode.setText(pickingOutStorageItem.getMaterielCode());
        viewHolder.materielDescribe.setText(pickingOutStorageItem.getMaterielDescription());
        viewHolder.canSendNumber.setText("可发" + pickingOutStorageItem.getCanSendNumber() + pickingOutStorageItem.getUnit());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(pickingOutStorageItem.getActualSendNumber()) ? "填写数量" : ("实发" + pickingOutStorageItem.getActualSendNumber() + pickingOutStorageItem.getUnit()));
        if (TextUtils.isEmpty(pickingOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStoragePickingDetailActivity.class);
                String serial = viewHolder.serialNumber.getText().toString();
                intent.putExtra(OutStorageExtra.SERIAL, serial);
                intent.putExtra(OutStorageExtra.PICKING_CLASSIFY, mPickingClassify);
                intent.putExtra(OutStorageExtra.USE_DEPARTMENT, mUseDepartment);
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, canSendNumber, outStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
